﻿namespace Blaze.Editor.EditorExtensions.HierarchyComponents
{
    using UnityEngine;

    /// <summary>
    /// 用于标记带TemplateTag的游戏对象。
    /// </summary>
    public class TemplateMark : HierarchyComponent
    {
        /// <summary>
        /// 当需要绘制组件时调用此方法。
        /// </summary>
        protected override void OnDraw(GameObject gameObject, Rect selectionRect)
        {
            GUI.color = Color.gray;
            GUI.DrawTexture(mRect, mTemplateIcon);
        }

        /// <summary>
        /// 当需要初始化组件时调用此方法。
        /// </summary>
        protected override void OnInitialize()
        {
            mTemplateIcon = BlazeEditorResources.Load<Texture2D>("icon_template_mark.png");
        }

        /// <summary>
        /// 当组件需要布局的时调用此方法。
        /// </summary>
        /// <param name="gameObject"></param>
        /// <param name="rect">剩余可用的空间</param>
        protected override bool OnLayout(GameObject gameObject, ref Rect rect)
        {
            if (rect.width < mRect.width)
                return false;
            mRect.x = rect.xMax - mRect.width;
            mRect.y = rect.y;
            rect = new Rect(rect)
            {
                width = rect.width - mRect.width,
            };
            return true;
        }

        /// <summary>
        /// 当需要验证组件是否显示时调用此方法。
        /// </summary>
        /// <param name="gameObject">游戏对象</param>
        /// <returns></returns>
        protected override bool OnValidate(GameObject gameObject)
        {
            return gameObject.CompareTag("Template");
        }

        private Rect mRect = new Rect(0, 0, 16, 16);
        private Texture2D mTemplateIcon;
    }
}